<?php

App::uses('AppModel', 'Model');

class Spot extends AppModel {
	
	/**
	 * Model name
	 *
	 * @var string
	 */
	public $name = "Spot";
	
	/**
	 * Display field
	 *
	 * @var string
	 */
	public $displayField = "name";
	
	/**
	 * Virtual fields
	 * @var array
	 */
	public $virtualFields = array (
		'category' => 'SELECT Category.name FROM categories AS Category WHERE Spot.category_id = Category.id',
		'city'     => 'SELECT City.name FROM cities AS City WHERE Spot.city_id = City.id',
		'score'    => 'SELECT IFNULL(AVG(Vote.rating),0) FROM votes AS Vote WHERE Spot.id = Vote.spot_id'
	);
	
	/**
	 * Model Behavior
	 * 
	 * @var array
	 */
	public $actsAs = array ( 'GeoLocation' );
	
	/**
	 * Validation rules
	 * @var array
	 */
	public $validate = array(
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	
	/**
	 * belongsTo associations
	 * @var array
	 */
	public $belongsTo = array(
		'Category' => array(
			'className' => 'Category',
			'foreignKey' => 'category_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'City' => array(
			'className' => 'City',
			'foreignKey' => 'city_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	/**
	 * hasMany associations
	 * @var array 
	 */
	public $hasMany = array(
        'Item' => array(
            'className'  => 'Item',
            'order'      => 'Item.name ASC'
        )
    );
	
}